import { Controller } from 'egg';
import * as moment from 'moment';
import { ReqUpdateOrder, UpdateStatus } from '../model/req_update_order';
import { UserRole } from '../model/user';

const VALIDATION_UPDATE_ORDERED_ORDER = {
    ordered_schedule_id: 'number',
    reason: {
        type: 'string',
        required: false,
        allowEmpty: true,
    },
    type: {
        type: 'integer',
        required: true,
        allowEmpty: false,
        min: 1,
        max: 5,
    },
    date: {
        type: 'string',
        required: false,
        allowEmpty: false,
    },
    time_from: {
        type: 'string',
        required: false,
        allowEmpty: false,
    },
    time_to: {
        type: 'string',
        required: false,
        allowEmpty: false,
    },
};

const VALIDATION_CONFIRM_REQ_UPDATE_ORDER = {
    req_update_order_id: 'number',
    status:  {
        type: 'int',
        min: 1, // 1: finished
        max: 2, // 2: canceled
        required: true,
        allowEmpty: false,
    },
};

export default class ReqUpdateOrderController extends Controller {
    /**
     * 学生请求修改自己的排期
     */
    public async updateOrderedOrder() {
        const { ctx, service } = this;
        ctx.validate(VALIDATION_UPDATE_ORDERED_ORDER);
        // todo： 检查请假申请
        await service.reqUpdateOrder.updateOrderedOrder(ctx.request.body as ReqUpdateOrder);
        return ctx.helper.success(ctx);
    }

    /**
     * 老师对学生申请更改时间
     */
    public async confirmReqUpdateOrder() {
        const { ctx, service } = this;
        if (ctx.user.role !== UserRole.Teacher) {
            ctx.helper.fail(ctx);
        }
        ctx.validate(VALIDATION_CONFIRM_REQ_UPDATE_ORDER);
        const { req_update_order_id, status } = ctx.request.body;
        if (status === UpdateStatus.Finished) {
            await service.reqUpdateOrder.confirmReqUpdateOrder(req_update_order_id, ctx.user.id);
        } else {
            await service.reqUpdateOrder.cancelReqUpdateOrder(req_update_order_id, ctx.user.id);
        }
        return ctx.helper.success(ctx);
    }

    public async getNeedConfirmReqUpdateOrder() {
        const { ctx, service } = this;
        if (ctx.user.role !== UserRole.Teacher) {
            // 老师查询需要自己确认的修改
            ctx.helper.invalidateParamsError('只有老师才能访问');
        }
        let result = await service.reqUpdateOrder.getNeedConfirmReqUpdateOrder(ctx.user.id);
        if (result) {
            result = result.map((item) => ({
                ...item,
                new_date: moment(item.new_date).format('YYYY年MM月DD日'),
                new_day: moment(item.new_date).day(),
                old_date: moment(item.old_date).format('YYYY年MM月DD日'),
                old_day: moment(item.old_date).day(),
                old_time_from: moment(item.old_time_from, 'HH:mm:SS').format('HH:mm'),
                old_time_to: moment(item.old_time_to, 'HH:mm:SS').format('HH:mm'),
                new_time_from: moment(item.new_time_from, 'HH:mm:SS').format('HH:mm'),
                new_time_to: moment(item.new_time_to, 'HH:mm:SS').format('HH:mm'),
            }));
        }
        ctx.helper.success(ctx, result);
    }
}
